package cn.javaxxw.springboot.mapper;

import cn.javaxxw.springboot.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 功能描述: 
 * @author  TuYong
 * @date  2022/5/30 15:36
 */
@Repository
public interface UserMapper {

    /**
     * 注解方式
     * 查询
     * @return
     */
    @Select("select * from user")
    List<User> getAll();

    /**
     * XML方式
     * 通过id查询
     * @param id
     * @return
     */
    User findById(@Param("id") Long id);

    /**
     * 注解方式
     * 插入
     * @param user
     * @return id
     */
    @Insert({ "insert into user(username,age,address,create_time,update_time)"
            + "values(#{user.username},#{user.age},#{user.address},#{user.createTime},#{user.updateTime})" })
    @SelectKey(resultType = long.class,keyColumn = "id",before = false,statement = "SELECT LAST_INSERT_ID() AS id",keyProperty = "user.id")
    Long insert(@Param("user") User user);
}
